<?php
/**
 * Created by PhpStorm.
 * User: dingjinlai
 * Date: 2019/6/25
 * Time: 下午11:28
 */

class Epurchase_orders_model extends CI_Model
{
    public function insert_batch($data)
    {
        $this->db->insert_batch('epurchase_orders', $data);
    }

    public function info($param = array())
    {
        $query = $this->db->from('epurchase_orders')->where($param)->get();

        $info = $query->row_array();

        return $info;
    }

    public function goods_insert_batch($data)
    {
        $this->db->insert_batch('epurchase_goods', $data);
    }

    public function salse_goods_count($param = array())
    {

        $start_time = $param['add_time_start'] ? strtotime($param['add_time_start']) : 0;
        $end_time = $param['add_time_end'] ? strtotime($param['add_time_end']) : 0;

        //采购数量
        $this->db->from('epurchase_goods as eg')
            ->select('count(distinct eg.goods_id) as eg_count')
            ->join('epurchase_orders as eo', 'eg.po_id = eo.po_id', 'left');

        if ($start_time && $end_time) {
            $this->db->where("eo.date_create >= '{$start_time}'");
            $this->db->where("eo.date_create <= '{$end_time}'");
        }

        $query = $this->db->get();
        $pg_count = $query->row_array();

        //下架数量
        $this->db->from('goods as g')->select('count(1) as goods_count')->where('g.is_on_sale = 0');

        if ($start_time && $end_time) {
            $this->db->where("g.on_sale_time >= '{$start_time}'");
            $this->db->where("g.on_sale_time <= '{$end_time}'");
        }

        $query = $this->db->get();
        $goods_count = $query->row_array();


        $supp_pg_count = array();
        $supp_goods_count = array();
        if ($start_time && $end_time) {

            //各个供应商采购数量
            $this->db->from('epurchase_goods as eg')
                ->select('count(distinct eg.goods_id) as eg_count, g.suppliers_id')
                ->join('goods as g', 'eg.goods_id = g.goods_id', 'left')
                ->join('epurchase_orders as eo', 'eg.po_id = eo.po_id', 'left');

            $this->db->where("eo.date_create >= '{$start_time}'");
            $this->db->where("eo.date_create <= '{$end_time}'");

            $this->db->group_by("g.suppliers_id");

            $query = $this->db->get();
            $supp_pg_count = $query->result_array();

            //各个供应商下架数量
            $supp = array_column($supp_pg_count, 'suppliers_id');

            $this->db->from('goods as g')
                ->select("count(1) as g_count, g.suppliers_id")
                ->join("suppliers as s", 'g.suppliers_id = s.suppliers_id', 'left')
                ->where_in('g.suppliers_id', $supp);

            $this->db->where("g.on_sale_time >= '{$start_time}'");
            $this->db->where("g.on_sale_time <= '{$end_time}'");

            $this->db->group_by("g.suppliers_id");

            $query = $this->db->get();
            $supp_goods_count = $query->result_array();
        }

        $data = array(
            'pg_count' => $pg_count['eg_count'],
            'goods_count' => $goods_count['goods_count'],
            'supp_pg_count' => $supp_pg_count,
            'supp_goods_count' => $supp_goods_count
        );

        return $data;
    }

    public function pg_count($param = array())
    {
        $this->db->from('epurchase_goods as pg')
            ->select('count(distinct g.goods_id) as goods_count')
            ->join('goods as g', 'pg.goods_id = g.goods_id', 'left')
            ->join('suppliers as s', 'g.suppliers_id = s.suppliers_id', 'left')
            ->join('epurchase_orders as po', 'pg.po_id = po.po_id', 'left');

        if ($param['add_time_start'] && $param['add_time_end']) {
            $start_time = strtotime($param['add_time_start']);
            $end_time = strtotime($param['add_time_end']);

            $this->db->where("po.date_create >= '{$start_time}'");
            $this->db->where("po.date_create <= '{$end_time}'");
        }

        if ($param['supp_id']) {
            $this->db->select('g.suppliers_id,s.suppliers_name');
            $this->db->where_in('g.suppliers_id', $param['supp_id']);

            $this->db->group_by('g.suppliers_id');
            $this->db->order_by('goods_count DESC');
        } else {
            $_old_time = strtotime('2015-01-01');
            $_new_time = strtotime('-7 day');
            $this->db->where('g.goods_id > 0');
            $this->db->select("INTERVAL(g.add_time, {$_old_time},{$_new_time}) as spe_count");
            $this->db->group_by('spe_count');
        }



        $query = $this->db->get();

        $goods = $query->result_array();

        return $goods;
    }
}
